/*
 * MDIAcademics.java
 *
 * Created on 29 de Outubro de 2008, 16:48
 */
package br.com.radaction.gui;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author  robson
 */
public class MDIAcademics extends javax.swing.JFrame {

    private static Connection conexao;
    private FormAvaliacao formAvaliacao;
    private FormDisciplina formDisciplina;
    private FormCurso formCurso;
    private FormUsuario formUsuario;
    private FormSobre formSobre;
    

    /** Creates new form MDIAcademics */
    public MDIAcademics() {
        initComponents();
    }

    public static Connection getConexao() {
        return conexao;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        mnAvaliacao = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        mnDisciplina = new javax.swing.JMenuItem();
        mnCurso = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mnUsuario = new javax.swing.JMenuItem();
        mnSobre = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RADAcademics");
        setFont(new java.awt.Font("Lucida Grande", 0, 10));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        fileMenu.setText("Agenda");

        mnAvaliacao.setText("Avaliações");
        mnAvaliacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnAvaliacaoActionPerformed(evt);
            }
        });
        fileMenu.add(mnAvaliacao);

        menuBar.add(fileMenu);

        jMenu1.setText("Cadastro");

        mnDisciplina.setText("Disciplinas");
        mnDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnDisciplinaActionPerformed(evt);
            }
        });
        jMenu1.add(mnDisciplina);

        mnCurso.setText("Cursos");
        mnCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnCursoActionPerformed(evt);
            }
        });
        jMenu1.add(mnCurso);

        menuBar.add(jMenu1);

        jMenu2.setText("Manutenção");

        mnUsuario.setText("Usuários");
        mnUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnUsuarioActionPerformed(evt);
            }
        });
        jMenu2.add(mnUsuario);

        menuBar.add(jMenu2);

        mnSobre.setText("RAD");

        jMenuItem1.setText("Sobre");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        mnSobre.add(jMenuItem1);

        menuBar.add(mnSobre);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 978, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    try {
        Class.forName("org.postgresql.Driver");
        MDIAcademics.conexao = DriverManager.getConnection("jdbc:postgresql://localhost/rad_academico", "postgres", "postgres");
        FormLogin dialog = new FormLogin(this, true);
        dialog.setVisible(true);
    } catch (SQLException ex) {
        JOptionPane.showInternalMessageDialog(this.desktopPane, String.format("Erro ao conectar ao SGBD: %s", ex.getMessage()), "Erro", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(MDIAcademics.class.getName()).log(Level.SEVERE, null, ex);
        System.exit(1);
    } catch (ClassNotFoundException ex) {
        JOptionPane.showInternalMessageDialog(this.desktopPane, "Erro ao carregar o Drive do SGBD", "Erro", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(MDIAcademics.class.getName()).log(Level.SEVERE, null, ex);
        System.exit(2);
    }
}//GEN-LAST:event_formWindowOpened

private void mnAvaliacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnAvaliacaoActionPerformed
    if (this.formAvaliacao == null) {
        this.formAvaliacao = new FormAvaliacao(this);
        this.desktopPane.add(this.formAvaliacao);
    }
    this.openJanela(this.formAvaliacao);
}//GEN-LAST:event_mnAvaliacaoActionPerformed

private void mnDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnDisciplinaActionPerformed
    if (this.formDisciplina == null) {
        this.formDisciplina = new FormDisciplina(this);
        this.desktopPane.add(this.formDisciplina);
    }
    this.openJanela(this.formDisciplina);
}//GEN-LAST:event_mnDisciplinaActionPerformed

private void mnCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnCursoActionPerformed
    if (this.formCurso == null) {
        this.formCurso = new FormCurso(this);
        this.desktopPane.add(this.formCurso);
    }
    this.openJanela(this.formCurso);
}//GEN-LAST:event_mnCursoActionPerformed

private void mnUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnUsuarioActionPerformed
 if (this.formUsuario == null) {
        this.formUsuario = new FormUsuario(this);
        this.desktopPane.add(this.formUsuario);
    }
    this.openJanela(this.formUsuario);
}//GEN-LAST:event_mnUsuarioActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
    if (this.formSobre == null) {
        this.formSobre = new FormSobre(this);
        this.desktopPane.add(this.formSobre);
    }
    this.openJanela(this.formSobre);
}//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])  {
        try {
            UIManager.setLookAndFeel("net.sourceforge.napkinlaf.NapkinLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MDIAcademics.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MDIAcademics.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MDIAcademics.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MDIAcademics.class.getName()).log(Level.SEVERE, null, ex);
        }
     
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MDIAcademics().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mnAvaliacao;
    private javax.swing.JMenuItem mnCurso;
    private javax.swing.JMenuItem mnDisciplina;
    private javax.swing.JMenu mnSobre;
    private javax.swing.JMenuItem mnUsuario;
    // End of variables declaration//GEN-END:variables

    private void openJanela(JComponent obj) {
        if (!obj.isVisible()) {
            if (!obj.isValid()) {
                desktopPane.add(obj);
            }
            obj.setVisible(true);
        }
    }
}
